layui.use(['form','jquery','tree','upload','element','slider'], function () {
    var $ = layui.jquery;
    var form = layui.form;
    var tree = layui.tree;
    var upload = layui.upload;
    var element = layui.element;
    var slider = layui.slider


    var indexAge;
    slider.render({
        elem: '#slideTest8'
        ,input: true //输入框
        ,setTips: function(value){ //自定义提示文本
            indexAge = value;
            return value + '岁';
        }
        ,change: function(value){
            $('#test-slider-tips1').html('年龄'+value+'岁');
        }
    });

    //保存个人信息1
    $("#updatePerson1").click(function () {
        var userid = $("#userid").val();
        var username1 = $("#username1").val();
        var description1 = $("#description1").val();
        var address1 = $("#address1").val();
        //获取单选框的值
        var userSex = $("input[name='sex']:checked").val();
        if (username1 == "" || username1.length == 0){
            layer.tips('请填写用户名', '#username1',{
                tips:[1,'#ea3323']
            });
        }else if (description1 == "" || description1.length == 0){
            layer.tips('请填写个人描述', '#description1',{
                tips:[1,'#ea3323']
            });
        }else if (address1 == "" || address1.length == 0){
            layer.tips('地址不能为空', '#address1',{
                tips:[1,'#ea3323']
            });
        }else{
            $.ajax({
                type: "post",
                data: {
                    "userId" : userid,
                    "username" : username1,
                    "description" : description1,
                    "address" : address1,
                    "sex": userSex
                },
                url: "/user/updatePerson/Information",
                success: function (res){
                    if (res.state == true){
                        layer.msg('保存成功',{time:1000},function (){
                            window.location.reload();
                        });
                    }else if (res.state == false){
                        layer.msg(res.msg);
                    }
                }
            })
        }
    })

    //常规使用 - 普通图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        ,url: '/file/upload/headImg' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）
            });

            element.progress('demo', '0%'); //进度条复位
            layer.msg('上传中', {icon: 16, time: 0});
        }
        ,done: function(res){
            //如果上传失败
            if(res.code != 0){
                return layer.msg(res.msg);
            }else{
                layer.msg("上传成功",{icon: 1});
            }
            //上传成功的一些操作
            //……
            $('#demoText').html(''); //置空上传失败的状态
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
        //进度条
        ,progress: function(n, elem, e){
            element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
        }
    });


    $("#updatePerson2").click(function(){
        var userid = $("#userid").val();
        var oldPassword = $("#oldPassword").val();
        var newPassword = $("#newPassword").val();
        var newPasswordCheck = $("#newPasswordCheck").val();
        if (oldPassword == "" || oldPassword.length == 0){
            layer.tips('请填写旧密码', '#oldPassword',{
                tips:[1,'#ea3323']
            });
        }else if (newPassword == "" || newPassword.length == 0){
            layer.tips('请填写新密码', '#newPassword',{
                tips:[1,'#ea3323']
            });
        }else if (newPasswordCheck == "" || newPasswordCheck.length == 0){
            layer.tips('请确认密码', '#newPasswordCheck',{
                tips:[1,'#ea3323']
            });
        }else if (newPasswordCheck != newPassword){
            layer.tips('两次密码不一致', '#newPasswordCheck',{
                tips:[1,'#ea3323']
            });
        }else if (newPassword.length < 6){
            layer.tips('新密码长度不符合,长度请大于或者等于6位', '#newPasswordCheck',{
                tips:[1,'#ea3323']
            });
        }else {
            $.ajax({
                type: "post",
                data: {
                    "userid" : userid,
                    "oldPassword" : oldPassword,
                    "newPassword" : newPassword,
                    "newPasswordCheck" : newPasswordCheck
                },
                url: "/user/updatePerson/updatePersonPassword",
                success: function (res){
                    if (res.state == false){
                        layer.msg(res.msg);
                    }else if (res.state==true){
                        layer.msg('修改成功',function (){
                            window.location.href = "/login";
                        });
                    }
                }
            })
        }
})
    //打开地址树
    $("#address1").click(function (){
        layer.open({
            type: 2,
            offset: ['70px', '320px'],
            title: '请选择地址',
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['600px', '350px'],
            content: '/user/address/tree',
        });

    })
    //修改密码

})